<?php
// No directAcces
defined('_AppBackMiage') or die('Restricted access');
if(isAdministrateur ()){
    include_once (DS . "backoffice/include/header.php");
    include_once( DS . "include/zones.php");

    if (isset($_GET['uid']) && !empty($_GET['uid']))
    {
        $entreprise = new Entreprise($_GET['uid']);

        if (isset($_POST['submit']))
        {
            // variable qui va nous servir pour savoir s'il y a a eu une erreur ;)
            $form = true;
            $errMSG = array();


            /* --------------------- INFORMATIONS ----------------------- */
            if (isset($_POST['lastname']) && !empty($_POST['lastname']))
            {
                $tmp = $entreprise->setNomEntreprise($_POST['lastname']);
                if ($tmp['boolOk'] === false)
                {
                    $errMSG['nom'] = $tmp['errmsg'];
                    $form = false;
                }
            }

            if (isset($_POST['description']) && !empty($_POST['description']))
            {
                $tmp = $entreprise->setDescriptionEntreprise($_POST['description']);
                if ($tmp['boolOk'] === false)
                {
                    $errMSG['description'] = $tmp['errmsg'];
                    $form = false;
                }
            }
            /* ------------------ FIN INFORMATIONS --------------------- */

            /* ----------------------- CONTACT ------------------------- */
            if (isset($_POST['email']) && !empty($_POST['email']))
            {
                $tmp = $entreprise->setMailEntreprise($_POST['email']);
                if ($tmp['boolOk'] === false)
                {
                    $errMSG['mail'] = $tmp['errmsg'];
                    $form = false;
                }
            }

            if (isset($_POST['tel']) && !empty($_POST['tel']))
            {
                $tmp = $entreprise->setTelephoneEntreprise($_POST['tel']);
                if ($tmp['boolOk'] === false)
                {
                    $errMSG['tel'] = $tmp['errmsg'];
                    $form = false;
                }
            }

            if (isset($_POST['site']) && !empty($_POST['site']))
            {
                $tmp = $entreprise->setSiteEntreprise($_POST['site']);
                if ($tmp['boolOk'] === false)
                {
                    $errMSG['site'] = $tmp['errmsg'];
                    $form = false;
                }
            }
            /* -------------------- FIN CONTACT ----------------------- */

            /* ----------------------- ADRESSE ------------------------ */
            if (isset($_POST['adresse']) && !empty($_POST['adresse']))
            {
                $tmp = $entreprise->setAdresse1Entreprise($_POST['adresse']);
                if ($tmp['boolOk'] === false)
                {
                    $errMSG['add1'] = $tmp['errmsg'];
                    $form = false;
                }
            }
            if (isset($_POST['adresse2']) && !empty($_POST['adresse2']))
            {
                $tmp = $entreprise->setAdresse2Entreprise($_POST['adresse2']);
                if ($tmp['boolOk'] === false)
                {
                    $errMSG['add2'] = $tmp['errmsg'];
                    $form = false;
                }
            }
            if (isset($_POST['city']) && !empty($_POST['city']))
            {
                $tmp = $entreprise->setVilleEntreprise($_POST['city']);
                if ($tmp['boolOk'] === false)
                {
                    $errMSG['ville'] = $tmp['errmsg'];
                    $form = false;
                }
            }
            if (isset($_POST['cp']) && !empty($_POST['cp']))
            {
                $tmp = $entreprise->setCPEntreprise($_POST['cp']);
                if ($tmp['boolOk'] === false)
                {
                    $errMSG['cp'] = $tmp['errmsg'];
                    $form = false;
                }
            }
            if (isset($_POST['zone']) && !empty($_POST['zone']))
            {
                $tmp = $entreprise->setIdZone($_POST['zone']);
                if ($tmp['boolOk'] === false)
                {
                    $errMSG['zone'] = $tmp['errmsg'];
                    $form = false;
                }
            }
            /* -------------------- FIN ADRESSE ----------------------- */

            if ($form === true)
            {
                $entreprise->updateEntreprise();
                // Redirection
                redirectTo(URLBACK."_userentreprise_view&uid=".$entreprise->getIdEntreprise());
            }
        }
    }
    else
    {
        redirectTo(URLBACK."_userentreprise_list");
    }
    ?>
    <?php
    include_once( DS . "backoffice/include/toppanel_utilisateurs.php");
    ?>
    <div id="wrapper">
        <div id="content">
            <div id="box">
                <h3 id="adduser">Edition d'une entreprise</h3>
                <form id="form" method="post">

                    <fieldset id="personal">
                        <legend>Informations</legend>

                        <label for="lastname">Nom : </label>
                        <input name="lastname" id="lastname" type="text" tabindex="1" value="<?php echo stripslashes($entreprise->getNomEntreprise()); ?>" />
                        <span class="error"><?php
    if (isset($errMSG['nom']))
    {
        echo $errMSG['nom'];
    }
    ?></span>

                        <br />
                        <label for="description">Description : </label>
                        <textarea name="description" id="description" type="text" tabindex="1"><?php echo $entreprise->getDescriptionEntreprise(); ?></textarea>
                        <span class="error"><?php
                            if (isset($errMSG['description']))
                            {
                                echo $errMSG['description'];
                            }
    ?></span>

                        <br />
                    </fieldset>

                    <fieldset id="personal">
                        <legend>Contact</legend>

                        <label for="tel">Telephone : </label>
                        <input name="tel" id="email" type="text"tabindex="2" value="<?php echo $entreprise->getTelephoneEntreprise(); ?>" />
                        <span class="error"><?php
                            if (isset($errMSG['tel']))
                            {
                                echo $errMSG['tel'];
                            }
    ?></span>

                        <br />
                        <label for="email">Email : </label>
                        <input name="email" id="email" type="text"tabindex="2" value="<?php echo $entreprise->getMailEntreprise(); ?>" />
                        <span class="error"><?php
                            if (isset($errMSG['email']))
                            {
                                echo $errMSG['email'];
                            } ?></span>

                        <br />
                        <label for="site">Site : </label>
                        <input name="site" id="site" type="text"tabindex="2" value="<?php echo $entreprise->getSiteEntreprise(); ?>" />
                        <span class="error"><?php
                            if (isset($errMSG['site']))
                            {
                                echo $errMSG['site'];
                            }
    ?></span>

                    </fieldset>

                    <fieldset id="address">
                        <legend>Adresse</legend>

                        <label for="adresse">Adresse : </label>
                        <input name="adresse" id="adresse" type="text" tabindex="1" value="<?php echo $entreprise->getAdresse1Entreprise(); ?>" />
                        <span class="error"><?php
                            if (isset($errMSG['adresse']))
                            {
                                echo $errMSG['adresse'];
                            }
    ?></span>

                        <br />
                        <label for="adresse2">Adresse + : </label>
                        <input name="adresse2" id="adresse2" type="text" tabindex="1" value="<?php echo $entreprise->getAdresse2Entreprise(); ?>" />
                        <span class="error"><?php
                            if (isset($errMSG['adresse2']))
                            {
                                echo $errMSG['adresse2'];
                            }
    ?></span>

                        <br />
                        <label for="cp">Code Postal : </label>
                        <input name="cp" id="cp" type="text" tabindex="1" value="<?php echo $entreprise->getCPEntreprise(); ?>" />
                        <span class="error"><?php
                            if (isset($errMSG['cp']))
                            {
                                echo $errMSG['cp'];
                            }
    ?></span>

                        <br />
                        <label for="city">Ville : </label>
                        <input name="city" id="city" type="text" tabindex="2" value="<?php echo $entreprise->getVilleEntreprise(); ?>" />
                        <span class="error"><?php
                            if (isset($errMSG['city']))
                            {
                                echo $errMSG['city'];
                            } ?></span>
                        <br />
                        <label for="zones">Situation : </label>

                        <b>France, Alpes-Maritimes/Monaco : </b>
                        <br/>
                        <div style="margin-left:140px;" id="situationGeo">
                            <?php
                            foreach ($zones as $idZone => $zone)
                            {
                                $str = "<b>" . $zone['nom'] . "</b><br/>(<i>";
                                $nbVille = 1;
                                foreach ($zone['ville'] as $idVille => $ville)
                                {
                                    if ($nbVille == count($zone['ville']))
                                    {
                                        $str .= $ville;
                                    }
                                    else
                                    {
                                        $str .= $ville . ", ";
                                    }
                                    $nbVille += 1;
                                }
                                $str .= "</i>)";
                            ?>
                            <input <?php
                                if ($idZone == $entreprise->getIdZone())
                                {
                                    echo 'checked';
                                }
                            ?> type="radio" name="zone" value="<?php echo $idZone; ?>"/> <?php echo $str; ?><br/>
    <?php
                            }
    ?>
                                            </div>
                                            <div style="margin-left:120px;" id="situation">
                                                <input <?php if (12 == $entreprise->getIdZone())
                            {
                                echo 'checked';
                            } ?> type="radio" name="zone" value="12"/> <b>Autre d&eacute;partement</b>
                                                <br/>
                                                <input <?php if (13 == $entreprise->getIdZone())
                            {
                                echo 'checked';
                            } ?> type="radio" name="zone" value="13"/> <b>Etranger</b>
                                            </div>
                                        </fieldset>
                                        <div align="center">
                                            <input id="button1" type="submit" name="submit" value="Valider" />
                                            <input id="button2" type="reset" />
                                        </div>
                                    </form>
                                </div>
                            </div>
    <?php
    include_once (DS . "backoffice/include/sidebar.php");
    include_once (DS . "backoffice/include/footer.php");
}
else
{
    redirectTo(URLBACK);
}
?>